package sketch.ast;

import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener;

import sketch.util.Checker;

public class GeneratorCodePrinter extends NaiveASTFlattener {
	
	//visit the CompilationUnit and produce a skeleton with holes
	//what the hole looks like? need to produce a place hold
	
	//replace the generator code with holes
	public final MethodDeclarationWithHoles methodHoles;
	
	public GeneratorCodePrinter(MethodDeclarationWithHoles methodHoles) {
		Checker.checkNull(methodHoles, "The method declaration could not be null!");
		this.methodHoles = methodHoles;
	}
	
	//XXX many cases are omitted
	@Override
	public boolean visit(VariableDeclarationStatement statement) {
		if(methodHoles.removes.contains(statement)) {
			return false;
		} else {
			return super.visit(statement);
		}
	}
	
	@Override
	public boolean visit(MethodInvocation methodInvocation) {
		//System.out.println(" visited ");
		//replace the method invocation with hole content
		if(methodHoles.maps.containsKey(methodInvocation)) {
			return this.visit(methodHoles.maps.get(methodInvocation));
			//return visit(methodHoles.receivers.get(methodInvocation));
		} else {
			//System.out.println("not contain: " + methodInvocation);
			return super.visit(methodInvocation);
		}
	}
	
	public boolean visit(Expression expr) {
		this.buffer.append(expr.toString());
		return false;
	}
	
	public void endVisit(Expression expr) {
		//do nothing
	}
	
	//not used currently
	public boolean visit(GeneratorHole hole) {
		this.buffer.append(hole.content);
		return false;
	}
	
	public void endVisit(GeneratorHole hole) {
		//empty here
	}
}